package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingPlanPageQueryDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    NursingPlanMapper nursingPlanMapper;
    @Autowired
    NursingProjectPlanMapper nursingProjectPlanMapper;
    /**
     * 根据名称和状态分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean<NursingPlanVo> PageQuery(NursingPlanPageQueryDto dto) {
        //开启分页查询
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        Page <NursingPlanVo> page = nursingPlanMapper.PageQuery(dto);
        //将page的信息封装到pagebean中
        PageBean<NursingPlanVo> pageBean = PageBean.of(page, NursingPlanVo.class);
        return pageBean;
    }

    /**
     * 添加护理计划
     *
     * @param dto
     */
    @Override
    @Transactional//对数据库进行了多次操作，所以用事物管理
    public void addNursingPlan(NursingPlanDto dto) {
        //添加护理计划，操作nursing_plan表 NursingPlanMapper
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setStatus(1);
        nursingPlan.setCreateTime(LocalDateTime.now());
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setCreateBy(1671403256519078138L);
        nursingPlan.setUpdateBy(1671403256519078138L);
        nursingPlanMapper.addNursingPlan(nursingPlan);//注意添加时，主键自增返回，否则下面拿不到这个id

        //添加护理计划护理项目的关系，操作nursing_project_plan表  NursingProjectPlanMapper
        List<NursingProjectPlanDto> projectPlans =dto.getProjectPlans();
        if (CollUtil.isEmpty(projectPlans)){
            return;
        }
        //遍历集合，添加多条关系
        projectPlans.forEach(nursingProjectPlanDto -> {
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(nursingProjectPlanDto, NursingProjectPlan.class);
            //关联护理计划的id
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlan.setCreateTime(LocalDateTime.now());
            nursingProjectPlan.setUpdateTime(LocalDateTime.now());
            nursingProjectPlan.setCreateBy(1671403256519078138L);
            nursingProjectPlan.setUpdateBy(1671403256519078138L);
            nursingProjectPlanMapper.insert(nursingProjectPlan);
        });
    }

    /**
     * 根据id查询护理计划
     *
     * @param id
     * @return
     */
    @Override
    @Transactional
    public NursingPlanVo getDetailById(Long id) {

        //先根据id查询护理计划
        NursingPlanVo nursingPlanVo=  nursingPlanMapper.selectById(id);
        //再根据护理计划id查询关系数据List
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);
        //将两部分数据合并
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 修改护理项目
     *
     * @param dto
     */
    @Override
    @Transactional
    public void updateNursingPlan(NursingPlanDto dto) {
        //更新护理计划nursing_plan,更新护理计划项目关系nursing_project_plan 先删再加
        NursingPlan nursingPlan = BeanUtil.toBean(dto, NursingPlan.class);
        nursingPlan.setUpdateTime(LocalDateTime.now());
        nursingPlan.setUpdateBy(1671403256519078138L);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        //更新护理计划项目关系nursing_project_plan 先删再加
        nursingProjectPlanMapper.DeleteByPlanId(dto.getId());
        //添加
        List<NursingProjectPlanDto> projectPlans =dto.getProjectPlans();
        if (CollUtil.isEmpty(projectPlans)){
            return;
        }
        //遍历集合，添加多条关系
        projectPlans.forEach(nursingProjectPlanDto -> {
            NursingProjectPlan nursingProjectPlan = BeanUtil.toBean(nursingProjectPlanDto, NursingProjectPlan.class);
            //关联护理计划的id
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlan.setCreateTime(LocalDateTime.now());
            nursingProjectPlan.setUpdateTime(LocalDateTime.now());
            nursingProjectPlan.setCreateBy(1671403256519078138L);
            nursingProjectPlan.setUpdateBy(1671403256519078138L);
            nursingProjectPlanMapper.insert(nursingProjectPlan);
        });

    }

    /**
     * 删除护理计划
     *
     * @param id
     */
    @Override
    @Transactional
    public void deleteNursingPlan(Long id) {
        //如果护理计划的状态是启用，则禁止删除
        NursingPlanVo nursingPlanVo = nursingPlanMapper.selectById(id);
        Integer status = nursingPlanVo.getStatus();
        if(status==null){
            throw new RuntimeException("数据有误，禁止删除");
        }
        if(status==1){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        nursingPlanMapper.deleteNursingPlan(id);
        nursingProjectPlanMapper.DeleteByPlanId(id);
    }

    /**
     * 启用，禁用护理计划
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingPlan nursingPlan = NursingPlan.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateBy(1671403256519078138L)
                .build();
        nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    /**
     * 获取所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllNursingPlan() {
        return nursingPlanMapper.getAll();
    }
}
